package com.sys.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sys.mapper.LogMapper;
import com.sys.pojo.Log;
import com.sys.service.LogService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ZJF
 * @since 2021-04-01
 */
@Service
public class LogServiceImpl extends ServiceImpl<LogMapper, Log> implements LogService {

    @Resource
    private LogMapper logMapper;

    @Override
    public IPage<Log> findListByPage(Log log) {
        IPage<Log> iPage = new Page<>(log.getPage(), log.getLimit());
        LambdaQueryWrapper<Log> queryWrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(log.getOperation())) {
            queryWrapper.like(Log::getOperation, log.getOperation());
        }
        return logMapper.selectPage(iPage, queryWrapper);
    }

    @Override
    public int add(Log log) {
        return logMapper.insert(log);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(List<Integer> id) {
        int size = id.size();
        int n = 0;
        for (Integer i : id) {
            n += logMapper.deleteById(i);
        }
        return n==size;
    }

}
